//
//  LogCatWindowHelper.m
//  LogCat
//
//  Created by 清风徐来 on 2024/1/8.
//

#import "LogCatWindowHelper.h"

@implementation LogCatWindowHelper

static LogCatWindowHelper *_sharedInstance;
+ (instancetype)shared {
    if(_sharedInstance == nil) {
        _sharedInstance = [[LogCatWindowHelper alloc] init];
    }
    return _sharedInstance;
}

+(instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [super allocWithZone:zone];
    });
    return _sharedInstance;
}

- (id)copyWithZone:(NSZone *)zone {
    return _sharedInstance;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    return _sharedInstance;
}

/// 获取当前的window
/// - Parameter window: 外部传入的window
- (UIWindow *)keyWindowForTransition:(UIWindow * _Nullable )window {
    UIWindow *keyWindow = window;
    if (!keyWindow) {
        keyWindow = [UIApplication sharedApplication].delegate.window;
    }
    if (keyWindow) {
        return keyWindow;
    }

    if (@available(iOS 13.0, *)) {
        if ([UIApplication sharedApplication].connectedScenes.count > 0) {
            for (UIWindowScene *scene in [UIApplication sharedApplication].connectedScenes) {
                if (scene.activationState == UISceneActivationStateForegroundActive) {
                    keyWindow = scene.windows.firstObject;
                    break;
                }
            }
        } else {
            keyWindow = [UIApplication sharedApplication].delegate.window;
        }
    }

    if (!keyWindow) {
        NSLog(@"Warning！未找到UIWindow keyWindow");
    }

    return keyWindow;
}

/// 切换根控制器
/// - Parameter newRootViewController: 新的根控制器
/// - Parameter window: window
- (void)changeRootViewControllerTo:(UIViewController *)newRootViewController window:(UIWindow *)window {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *keyWindow = [self keyWindowForTransition:window];

        if (!keyWindow) {
            return;
        }

        [UIView transitionWithView:keyWindow
                          duration:0.25
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
            BOOL oldState = [UIView areAnimationsEnabled];
            [UIView setAnimationsEnabled:NO];
            keyWindow.rootViewController = newRootViewController;
            [UIView setAnimationsEnabled:oldState];
        } completion:nil];
    });
}

/// 跳转控制器
/// - Parameter newRootViewController: 新控制器
/// - Parameter window: window description
- (void)pushRootViewControllerTo:(UIViewController *)newRootViewController window:(UIWindow *)window {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *keyWindow = [self keyWindowForTransition:window];

        if (!keyWindow) {
            return;
        }
        [UIView transitionWithView:keyWindow
                          duration:0.25
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
            BOOL oldState = [UIView areAnimationsEnabled];
            [UIView setAnimationsEnabled:NO];
            if (keyWindow.rootViewController.navigationController) {
                [keyWindow.rootViewController.navigationController pushViewController:newRootViewController animated:YES];
            }
            [UIView setAnimationsEnabled:oldState];
        } completion:nil];
    });
}


@end
